<template>
    <a-drawer
        :title="formData.uid ? '编辑itest用户表' : '增加itest用户表'"
        :width="600"
        :visible="visible"
        :destroy-on-close="true"
        :footer-style="{ textAlign: 'right' }"
        @close="onClose"
    >
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="用户名, 登录用户名：" name="username">
                <a-input v-model:value="formData.username" placeholder="请输入用户名, 登录用户名" allow-clear />
            </a-form-item>
            <a-form-item label="密码：" name="userpass">
                <a-input v-model:value="formData.userpass" placeholder="请输入密码" allow-clear />
            </a-form-item>
            <a-form-item label="姓名：" name="realname">
                <a-input v-model:value="formData.realname" placeholder="请输入姓名" allow-clear />
            </a-form-item>
            <a-form-item label="用户角色id，关联base_user_group表的主键：" name="usergroupid">
                <a-input v-model:value="formData.usergroupid" placeholder="请输入用户角色id，关联base_user_group表的主键" allow-clear />
            </a-form-item>
            <a-form-item label="冗余字段，关联base_user_group表的数据的userType字段，用户类型 =1学生 =10教师 =20管理员：" name="usertype">
                <a-input v-model:value="formData.usertype" placeholder="请输入冗余字段，关联base_user_group表的数据的userType字段，用户类型 =1学生 =10教师 =20管理员" allow-clear />
            </a-form-item>
            <a-form-item label="用户头像地址：" name="userpic">
                <a-input v-model:value="formData.userpic" placeholder="请输入用户头像地址" allow-clear />
            </a-form-item>
            <a-form-item label="用户编码，多半是学号，导入用户数据时可能需要匹配这个字段：" name="usercode">
                <a-input v-model:value="formData.usercode" placeholder="请输入用户编码，多半是学号，导入用户数据时可能需要匹配这个字段" allow-clear />
            </a-form-item>
            <a-form-item label="用户编码类型 =0直接注册  =1学号导入， 还可以支持联合网站登录：" name="usercodetype">
                <a-input v-model:value="formData.usercodetype" placeholder="请输入用户编码类型 =0直接注册  =1学号导入， 还可以支持联合网站登录" allow-clear />
            </a-form-item>
            <a-form-item label="班级ID, 学生所属的班级，非必须，学生导入的第一个班级，关联cls表：" name="clsid">
                <a-input v-model:value="formData.clsid" placeholder="请输入班级ID, 学生所属的班级，非必须，学生导入的第一个班级，关联cls表" allow-clear />
            </a-form-item>
            <a-form-item label="年级ID, 非必须，关联grade表， TODO：有没有留级的学生关联多个grade：" name="gradeid">
                <a-input v-model:value="formData.gradeid" placeholder="请输入年级ID, 非必须，关联grade表， TODO：有没有留级的学生关联多个grade" allow-clear />
            </a-form-item>
            <a-form-item label="学院/系ID，非必须，关联faculty表：" name="facultyid">
                <a-input v-model:value="formData.facultyid" placeholder="请输入学院/系ID，非必须，关联faculty表" allow-clear />
            </a-form-item>
            <a-form-item label="校区ID，非必须，关联campus表：" name="campusid">
                <a-input v-model:value="formData.campusid" placeholder="请输入校区ID，非必须，关联campus表" allow-clear />
            </a-form-item>
            <a-form-item label="学校ID,关联school表：" name="schoolid">
                <a-input v-model:value="formData.schoolid" placeholder="请输入学校ID,关联school表" allow-clear />
            </a-form-item>
            <a-form-item label="班级id集合：" name="clsesids">
                <a-input v-model:value="formData.clsesids" placeholder="请输入班级id集合" allow-clear />
            </a-form-item>
            <a-form-item label="性别: 0:未填 1:男 2:女：" name="sex">
                <a-input v-model:value="formData.sex" placeholder="请输入性别: 0:未填 1:男 2:女" allow-clear />
            </a-form-item>
            <a-form-item label="教师科目:  0:未选科目 1:英语...：" name="course">
                <a-input v-model:value="formData.course" placeholder="请输入教师科目:  0:未选科目 1:英语..." allow-clear />
            </a-form-item>
            <a-form-item label="学生验证码激活后的到期的加密后的时间：" name="validitydate">
                <a-input v-model:value="formData.validitydate" placeholder="请输入学生验证码激活后的到期的加密后的时间" allow-clear />
            </a-form-item>
            <a-form-item label="手机号：" name="mobile">
                <a-input v-model:value="formData.mobile" placeholder="请输入手机号" allow-clear />
            </a-form-item>
            <a-form-item label="email：" name="email">
                <a-input v-model:value="formData.email" placeholder="请输入email" allow-clear />
            </a-form-item>
            <a-form-item label="固定电话：" name="tel">
                <a-input v-model:value="formData.tel" placeholder="请输入固定电话" allow-clear />
            </a-form-item>
            <a-form-item label="<此字段暂时用来存储学生是否有学校考试，请勿删除！>：" name="weixin">
                <a-input v-model:value="formData.weixin" placeholder="请输入<此字段暂时用来存储学生是否有学校考试，请勿删除！>" allow-clear />
            </a-form-item>
            <a-form-item label="状态 1:正常 0:隐藏/锁定/停用：" name="status">
                <a-input v-model:value="formData.status" placeholder="请输入状态 1:正常 0:隐藏/锁定/停用" allow-clear />
            </a-form-item>
            <a-form-item label="首次登录的标识0未登录，1首次登录 2登录后未修改信息3登录后为修改密码4都改完了：" name="stepflag">
                <a-input v-model:value="formData.stepflag" placeholder="请输入首次登录的标识0未登录，1首次登录 2登录后未修改信息3登录后为修改密码4都改完了" allow-clear />
            </a-form-item>
            <a-form-item label="创建时间：" name="createtime">
                <a-date-picker v-model:value="formData.createtime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择创建时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="创建人ID：" name="createuid">
                <a-input v-model:value="formData.createuid" placeholder="请输入创建人ID" allow-clear />
            </a-form-item>
            <a-form-item label="更新时间：" name="updatetime">
                <a-date-picker v-model:value="formData.updatetime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择更新时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="更新人ID：" name="updateuid">
                <a-input v-model:value="formData.updateuid" placeholder="请输入更新人ID" allow-clear />
            </a-form-item>
            <a-form-item label="假删除标识：" name="delflag">
                <a-input v-model:value="formData.delflag" placeholder="请输入假删除标识" allow-clear />
            </a-form-item>
            <a-form-item label="是否阅读用户协议 0未读 1已读：" name="agreementflag">
                <a-input v-model:value="formData.agreementflag" placeholder="请输入是否阅读用户协议 0未读 1已读" allow-clear />
            </a-form-item>
            <a-form-item label="教师认证码：" name="authcode">
                <a-input v-model:value="formData.authcode" placeholder="请输入教师认证码" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
        </template>
    </a-drawer>
</template>

<script setup name="itBaseUserForm">
    import { cloneDeep } from 'lodash-es'
    import { required } from '@/utils/formRules'
    import itBaseUserApi from '@/api/biz/itBaseUserApi'
    // 抽屉状态
    const visible = ref(false)
    const emit = defineEmits({ successful: null })
    const formRef = ref()
    // 表单数据
    const formData = ref({})
    const submitLoading = ref(false)

    // 打开抽屉
    const onOpen = (record) => {
        visible.value = true
        if (record) {
            let recordData = cloneDeep(record)
            formData.value = Object.assign({}, recordData)
        }
    }
    // 关闭抽屉
    const onClose = () => {
        formRef.value.resetFields()
        formData.value = {}
        visible.value = false
    }
    // 默认要校验的
    const formRules = {
    }
    // 验证并提交数据
    const onSubmit = () => {
        formRef.value
            .validate()
            .then(() => {
                submitLoading.value = true
                const formDataParam = cloneDeep(formData.value)
                itBaseUserApi
                    .itBaseUserSubmitForm(formDataParam, formDataParam.uid)
                    .then(() => {
                        onClose()
                        emit('successful')
                    })
                    .finally(() => {
                        submitLoading.value = false
                    })
            })
    }
    // 抛出函数
    defineExpose({
        onOpen
    })
</script>
